#!/usr/bin/env python
# encoding: utf-8
"""
@author: youfeng
@email: youfeng243@163.com
@license: Apache Licence
@file: copy_data.py
@time: 2018/2/6 17:07
"""
from common import util
from common.mongo import MongDb
from logger import Logger

APP_DATA_CONF = {
    'host': '172.16.215.16',
    'port': 40042,
    'db': 'app_data',
    'username': 'work',
    'password': 'haizhi'
}

# 日志模块
log = Logger('send_online_beanstalk.log').get_logger()

app_data_db = MongDb(APP_DATA_CONF['host'], APP_DATA_CONF['port'], APP_DATA_CONF['db'],
                     APP_DATA_CONF['username'],
                     APP_DATA_CONF['password'], log=log)


def main():
    source_table = 'enterprise_data_gov_new_2018_01_08'
    target_table = 'enterprise_data_gov'

    count = 0
    total = 0
    result_list = []
    with open('./copy_list.txt', 'r') as p_file:
        for line in p_file:
            total += 1
            company = line.strip().strip('\n').strip('\r')
            item = app_data_db.find_one(source_table, {'company': company})
            if item is None:
                log.error("当前企业没有找到: company = {}".format(company))
                continue

            item.pop('_id')

            count += 1
            item['_utime'] = util.get_now_time()
            result_list.append(item)
            if len(result_list) >= 200:
                app_data_db.insert_batch_data(target_table, result_list, key='_record_id')
                del result_list[:]

            if count % 10 == 0:
                log.info("当前进度: count = {}".format(count))

    if len(result_list) > 0:
        app_data_db.insert_batch_data(target_table, result_list, key='_record_id')
        del result_list[:]

    log.info("复制数据完成...")


if __name__ == '__main__':
    main()
